﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Bifrost;
using DevComponents.AdvTree;
using System.Drawing;
using System.Runtime.InteropServices;

namespace TextEditor.TextDocument.Data.Knowledge
{
    public sealed class KnowledgeBase : Singleton<KnowledgeBase>
    {
        public KnowledgeControl showKnowledgeControl = new KnowledgeControl();
        KnowledgeData knowDate = new KnowledgeData();
        public bool GetKeyParam(string keyStr, Point p, int width, int height)
        {
            this.showKnowledgeControl.IniKnowledgeTree.Nodes.Clear();
            DataRow[] rows = knowDate.DtParent.Select("NAME = '" + keyStr + "'");
            if (rows.Length > 0)
            {
                foreach (DataRow varRow in rows)
                {
                    Node n = new Node();
                    n.Text = varRow["name"].ToString();
                    n.Tag = varRow["id"].ToString();
                    DataRow[] childRows = knowDate.DtChildren.Select("SIMPLEMETAID_F='" + varRow["id"] + "'");
                    if (childRows.Length > 0)
                    {
                        foreach (DataRow varChildRow in childRows)
                        {
                            Node childNode = new Node();
                            childNode.Text = varChildRow["EVALUE"].ToString();
                            childNode.Tag = varChildRow["item_style"].ToString();
                            n.Nodes.Add(childNode);
                        }
                    }
                    showKnowledgeControl.IniKnowledgeTree.Nodes.Add(n);
                    n.Expand();
                }
                showKnowledgeControl.updatePoint(p, width, height);
                showKnowledgeControl.Text = keyStr;
                //showKnowledgeControl.Visible = true;
                ShowWindow(this.showKnowledgeControl.Handle, 4);
                return true;
            }
            return false;
        }

        public void RefreshData()
        {
            knowDate.RefreshData();
        }
        //弹出无焦点窗体
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hWnd, short cmdShow);
    }
}
